import Vue from 'vue'
import VueRouter from 'vue-router'
import RegiSter from '../views/RegiSter/RegiSter.vue'
Vue.use(VueRouter)


const originPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originPush.call(this, location).catch(err => err)
}

const routes = [{
  path: "/",
  redirect: "/RegiSter"
},
{
  path: "/RegiSter",
  name: "RegiSter",
  component: RegiSter,
  meta: {
    // 设置自定义属性
    isShow: false,
  }
},
{
  path: '/NaviGation',
  name: 'NaviGation',
  component: () => import('../views/NaviGation/NaviGation.vue'),
  meta: {
    // 设置自定义属性
    isShow: true,
  },
  children: [{
    path: "/",
    redirect: "/NaviGation/HomePage"
  },
  {
    path: "/NaviGation/HomePage",
    name: "HomePage",
    component: () => import('../views/HomePage/HomePage.vue'),
    meta: {
      isShow: true,
      title: "首页"
    }
  },
  {
    path: "/NaviGation/CateGory",
    name: "CateGory",
    component: () => import('../views/CateGory/CateGory.vue'),
    meta: {
      isShow: true,
      title: "导航路由二级文件"
    },
    children: [{
      path: "/NaviGation/CateGory/CateSub",
      name: "CateSub",
      component: () => import('../views/CateGory/comment/CateSub.vue'),
      meta: {
        isShow: true,
        title: "导航路由三级文件"
      },
      children: [{
        path: "/NaviGation/CateGory/CateSub",
        name: "CateSub",
        component: () => import('../views/CateGory/comment/CateSub.vue'),
      }]
    },
    {
      path: "/NaviGation/PartiCulars",
      name: "PartiCulars",
      component: () => import('../views/PartiCulars/PartiCulars.vue'),
      meta: {
        isShow: true,
        title: "详情页"
      }
    },
    {
      path: "/NaviGation/OrderForm",
      name: "OrderForm",
      component: () => import('../views/OrderForm/OrderForm.vue'),
      meta: {
        isShow: true,
        title: "订单文件"
      },
      children: [
        {
          path: "/",
          redirect: "/NaviGation/OrderForm/my"
        },
        {
          path: "/NaviGation/OrderForm/my",
          name: "my",
          component: () => import('../views/OrderForm/component/IView.vue'),
        },
        {
          path: "/NaviGation/OrderForm/order",
          name: "order",
          component: () => import('../views/OrderForm/component/orderBody.vue'),
          meta: {
            isShow: true,
            title: "我的订单"
          },
        },
        {
          path: "/NaviGation/OrderForm/details",
          name: "details",
          component: () => import('../views/OrderForm/component/DetailsView.vue'),
          meta: {
            isShow: true,
            title: "订单详情"
          },

        }]
    }, {
      path: "/NaviGation/PartiCulars",
      name: "PartiCulars",
      component: () => import('../views/PartiCulars/PartiCulars.vue'),
      meta: {
        isShow: true,
        title: "详情页"
      }
    }, {
      path: "/NaviGation/OrderForm",
      name: "OrderForm",
      component: () => import('../views/OrderForm/OrderForm.vue'),
      meta: {
        isShow: true,
        title: "订单文件"
      }
    }, {
      path: "/NaviGation/ShopPing",
      name: "ShopPing",
      component: () => import('../views/ShopPing/ShopPing.vue'),
      meta: {
        isShow: true,
        title: "购物车文件"
      },
    },
    {
      path: "/NaviGation/SiteView",
      name: "SiteView",
      component: () => import('../views/ShopPing/SiteView.vue'),
      meta: {
        isShow: true,
        title: "购物车地址页"
      },
      }
    ]
  },
  {
    path: "/NaviGation/ShopPing",
    name: "ShopPing",
    component: () => import('../views/ShopPing/ShopPing.vue'),
    meta: {
      isShow: true,
      title: "购物车文件"
    }
  }]
}]


const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.isShow) {
    const token = localStorage.getItem("token") || null;
    if (token) {
      next()
    } else {
      next("/")
    }
  } else {
    next()
  }
})




// 解决编程式路由往同一地址跳转时会报错的情况
const originalPush = VueRouter.prototype.push;
const originalReplace = VueRouter.prototype.replace;

// push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalPush.call(this, location, onResolve, onReject);
  return originalPush.call(this, location).catch(err => err);
};

//replace
VueRouter.prototype.replace = function push(location, onResolve, onReject) {
  if (onResolve || onReject)
    return originalReplace.call(this, location, onResolve, onReject);
  return originalReplace.call(this, location).catch(err => err);
};



















export default router